package org.azmat.student.Controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.azmat.student.ResultVO.Result;
import org.azmat.student.SQL.Entity.Student;
import org.azmat.student.SQL.Service.IStudentService;
import org.azmat.student.SQL.Service.Impl.StudentServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "学生管理", description = "学生管理相关接口")
@RestController
@RequestMapping("/students")
public class StudentController {
    @Autowired
    IStudentService studentService;
    @Autowired
    StudentServiceImpl studentServiceImpl;

    @GetMapping()
    @Operation(summary = "获取学生列表", description = "返回所有学生的列表")
    public Result<?> getStudentList(@Parameter(description = "页码", required = true) @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                                    @Parameter(description = "页容量", required = true) @RequestParam(name = "pageSize", defaultValue = "10") Integer PageSize,
                                    @Parameter(description = "搜索关键词", required = false) @RequestParam(defaultValue = "", required = false) String search) {
        Page<Student> page = (Page<Student>) studentServiceImpl.selectPage(pageNum, PageSize, search);
        return Result.success(page, "获取成功");
    }

    @Operation(summary = "添加学生", description = "添加一个学生")
    @PostMapping()
    public Result<?> addStudent(@RequestBody Student student) {
        studentService.save(student);
        return Result.success("添加成功");
    }

    @Operation(summary = "更新学生", description = "更新学生的信息")
    @PutMapping()
    public Result<?> updateStudent(@RequestBody Student student) {
        studentService.updateById(student);
        return Result.success("更新成功");
    }

    @Operation(summary = "删除学生", description = "删除一个学生")
    @DeleteMapping("/{id}")
    public Result<?> deleteStudent(@Parameter(description = "学生ID", required = true) @PathVariable("id") Long id) {
        studentService.removeById(id);
        return Result.success("删除成功");
    }
}
